package cn.hp.controller;

import cn.hp.bean.MasterAddress;
import cn.hp.service.MasterService;
import cn.hp.service.OrderService;
import cn.hp.utils.JsonData;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/master")
@Transactional
public class MasterController {

    @Autowired
    private MasterService masterService;


    @Autowired
    private OrderService orderService;

    /**
     * 查询所有技师
     * @param name
     * @param phone
     * @param page
     * @param limit
     */
    @RequestMapping("/selectList")
    @ResponseBody
    public JsonData selectList(String name, String phone, Integer page, Integer limit){
        Map<String,Object> map=new HashMap<String,Object>();
        if(!StringUtils.isEmpty(name)){
            map.put("name",name);
        }
        if(!StringUtils.isEmpty(phone)){
            map.put("phone",phone);
        }
        JsonData jsonData=masterService.findMatsers(map,page,limit);
        return jsonData;
    }

    /**
     * 查询所有的技师
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectMasterList")
    @ResponseBody
    public JsonData selectMasterList(Integer page,Integer limit){
        JsonData jsonData=masterService.selectMasterList(page,limit);
        return jsonData;
    }

    /**
     * 派单
     */
    @RequestMapping("/sendMaster")
    @ResponseBody
    public JsonData sendMaster(Integer id,Integer mid){
        MasterAddress masterAddress=masterService.findByMid(mid);
        if(masterAddress!=null&&masterAddress.getStatus().equals("1")){//判断技师是否在忙
            int Order=orderService.updateOrderStatus(id);
            if(Order>0){//判断顾客的订单状态是否修改成功
                int master=masterService.updataMasterAddressStatus(mid);
                if(master>0){//判断技师的订单状态是否修改成功
                    return JsonData.buildSuc("派单成功");
                }
            }
        }else {
            return JsonData.buildError("技师正在忙碌");
        }
        return JsonData.buildError("数据出错了哦");
    }

}
